import Vue from 'vue'
import Vuex from 'vuex'
import {Get,Post} from '../api'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    /*登录信息 */
    loginData:{},
  },
  getters: {
    getLoginInfo(state){
      return state.loginData.login;
    },
    getUserRole(state){
      return state.loginData.role;
    },
  },
  mutations: {
    setState: function(state, {key, value}) {
      state[key] = value;
    },
  },
  actions: {
    async login({commit},{username,password}){
      if (username.trim() === "" && password.trim() === "") {
        throw new Error("参数为空");
      }
      let res = await Post.login({username,password});
      if (res.statusCode === 1) {
        commit('setState',{key:'loginData',value:res.data});
      }
      return res;
    },
    async getLoginInfo({commit}){
      let res = await Get.getLoginInfo();

      if (res.statusCode === 1) {
        commit('setState',{key:'loginData',value:res.data});
      }
      
      return res;
    },
  }
})
